package com.wude.guild.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.mc.developmentkit.utils.ToastUtil;
import com.umeng.analytics.MobclickAgent;
import com.wude.guild.OperatorManagerActivity;
import com.wude.guild.R;
import com.wude.guild.WebActivity;
import com.wude.guild.servercore.mints.TitleBaseFragment;
import com.wude.http.HttpCom;
import com.wude.tools.Utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/22 0022.
 */

@SuppressLint("HandlerLeak")
public class BindCardFragment extends TitleBaseFragment implements View.OnClickListener {
    private LinearLayout ll_content;
    private TextView tv_text1;
    private TextView tv_text2;
    private TextView tv_goWeb;//立即申请
    private String jumpUrl;//申请卡的url
    private TextView tv_getAuthCode;//发送验证码
    private CountDownTimer timer;//验证码倒计时
    private EditText et_phone;
    private TextView tv_bind;//绑定
    private EditText et_authCode;//验证码
    private TextView tv_getGift;//领取礼包

    private OperatorManagerActivity activity;
    private String tag;

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.bind_card_fragment, container, false);
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("BindCardFragment");
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("BindCardFragment");
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    @Override
    public void onEnter(Object data) {
        super.onEnter(data);
        tag = (String) data;
    }

    private void initView(View view) {
        getTitleHeaderBar().getTitleTextView().setVisibility(View.GONE);
        ll_content = view.findViewById(R.id.ll_content);
        tv_text1 = view.findViewById(R.id.tv_text1);
        tv_text2 = view.findViewById(R.id.tv_text2);
        tv_goWeb = view.findViewById(R.id.tv_goWeb);
        tv_getAuthCode = view.findViewById(R.id.tv_getAuthCode);
        et_phone = view.findViewById(R.id.et_phone);
        tv_bind = view.findViewById(R.id.tv_bind);
        et_authCode = view.findViewById(R.id.et_authCode);
        tv_getGift = view.findViewById(R.id.tv_getGift);

        Map<String, String> dataMap = new HashMap<>();
        dataMap.put("operator", tag);
        HttpCom.POST(handler, HttpCom.getGetActivityText(), dataMap, false);

        timer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long l) {
                tv_getAuthCode.setText(l / 1000 + "秒后重试");
            }

            @Override
            public void onFinish() {
                tv_getAuthCode.setText("重新发送");
                tv_getAuthCode.setClickable(true);
            }
        };

        tv_goWeb.setOnClickListener(this);
        tv_getAuthCode.setOnClickListener(this);
        tv_bind.setOnClickListener(this);
        tv_getGift.setOnClickListener(this);
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        JSONArray data = jsonObject.getJSONArray("data");
                        tv_text1.setText(data.get(0).toString());
                        tv_text2.setText(data.get(1).toString());
                        jumpUrl = data.get(2).toString();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    ll_content.setVisibility(View.VISIBLE);
                    break;
                case 2:
                    ToastUtils.showLongToast("网络异常！");
                    break;
            }
        }
    };

    /**
     * 判断手机号发送验证码
     */

    Handler codeHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        int status = jsonObject.getInt("status");
                        String return_code = jsonObject.getString("return_code");
                        ToastUtil.showToast(return_code);
                        if (status == 1) {
                            //验证码发送成功
                            timer.start();
                            tv_getAuthCode.setClickable(false);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        Log.e("解析验证码异常", e.toString());
                    }
                    Log.e("发送验证码json", msg.obj.toString());
                    break;
                case 2:

                    break;
            }
        }
    };

    /**
     * 判断手机号发送验证码
     */
    Handler bindHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        int status = jsonObject.getInt("status");
                        String return_code = jsonObject.getString("return_code");
                        ToastUtil.showToast(return_code);
                        if (status == 1) {//绑卡成功
                            jumpActivity();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        Log.e("解析验证码异常", e.toString());
                    }

                    break;
                case 2:

                    break;
            }
        }
    };

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity = (OperatorManagerActivity) activity;
    }

    private void jumpActivity() {
        String phone = et_phone.getText().toString();
        Map<String, String> dataMap = new HashMap<>();
        dataMap.put("tag", tag);
        dataMap.put("phone", phone);
        activity.goToFragment(PayTelecomFragment.class, dataMap);
    }

    @Override
    public void onClick(View view) {
        String phone = et_phone.getText().toString();
        HashMap<String, String> map;
        switch (view.getId()) {
            case R.id.tv_goWeb://跳转web界面
                Intent intent = new Intent(getActivity(), WebActivity.class);
                intent.putExtra("url", jumpUrl);
                startActivity(intent);
                break;
            case R.id.tv_getAuthCode:
                if (TextUtils.isEmpty(phone)) {
                    ToastUtil.showToast("请输入手机号码！");
                    break;
                }
                map = new HashMap<>();
                map.put("token", Utils.getLoginUser().token);
                map.put("phone", phone);
                map.put("type", "1");
                HttpCom.POST(codeHandler, HttpCom.getBindPhoneAuthCode(), map, false);
                break;
            case R.id.tv_bind:
                String authCode = et_authCode.getText().toString();
                if (TextUtils.isEmpty(authCode)) {
                    ToastUtil.showToast("请输入验证码！");
                    break;
                }
                if (TextUtils.isEmpty(phone)) {
                    ToastUtil.showToast("请输入手机号码！");
                    break;
                }

                map = new HashMap<>();
                map.put("token", Utils.getLoginUser().token);
                map.put("phone", phone);
                map.put("code", authCode);
                HttpCom.POST(bindHandler, HttpCom.getBindPhone(), map, false);
//                jumpActivity();
                break;
            case R.id.tv_getGift://领礼包
                activity.pushFragmentToBackStack(GiftFragment.class, null);
                break;
        }
    }
}
